﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;
using Maticsoft.Common.DEncrypt;
using System.Data;

namespace Maticsoft.Web
{
    public partial class Login : System.Web.UI.Page
    {
        string retUrl = "default.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["retUrl"]))
            {
                retUrl = Request.QueryString["retUrl"];
            }

            if (!IsPostBack)
            {
                HttpCookie cookie = Request.Cookies["isaachome"];
                if (cookie != null)
                {
                    txtMobile.Value = DEncrypt.Decrypt(cookie["username"]);
                    cbRemember.Checked = true;
                }

                GetAdvert();
            }
        }

        protected void GetAdvert()
        {
            BLL.AdvertPics advBLL = new BLL.AdvertPics();
            DataSet ds = advBLL.GetList(1, "AdvertId=2 and IsActive=1", "OrderNo asc");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                loginAdvert.Src = "/uploadfiles/ads/" + ds.Tables[0].Rows[0]["pic"].ToString();
                loginAdvert.Alt = ds.Tables[0].Rows[0]["Title"].ToString();
                loginAdvert.Attributes.Add("onclick", "window.location.href='" + ds.Tables[0].Rows[0]["url"].ToString() + "'");
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtMobile.Value.Trim();
            string pwd = DEncrypt.Encrypt(txtPassword.Value.Trim());
            BLL.Members meBLL = new BLL.Members();
            Model.Members meModel = meBLL.GetModel(userName, pwd);
            if (meModel == null)
            {
                MessageBox.Show(this.Page, "用户名或者密码错误");
                return;
            }

            if (meModel.IsActive == 0)
            {
                MessageBox.Show(this.Page, "用户名或者密码错误");
                return;
            }

            if (meModel.Status == 0)
            {
                MessageBox.Show(this.Page, "账号未激活");
                return;
            }

            Session["UserId"] = meModel.UserId;
            Session["ManagerName"] = meModel.UserName;
            Session["GroupId"] = meModel.GroupId;
            Session["LevelId"] = meModel.LevelId;

            if (cbRemember.Checked)
            {
                HttpCookie cookie = new HttpCookie("isaachome");
                cookie.Values.Add("username", DEncrypt.Encrypt(meModel.UserName));
                cookie.Expires = DateTime.Now.AddYears(1);
                Response.Cookies.Add(cookie);
            }
            else
            {
                HttpCookie cookie = Request.Cookies["isaachome"];
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    Response.Cookies.Add(cookie);
                }
            }

            Response.Redirect(retUrl);
        }
    }
}